var app=getApp();
var ux = require('../../../../lib/ux.js');
var animation;
Page({

  /**
   * 页面的初始数据
   */
  data: {   
    gname:'',
    id:null,
    details:'',
    userName:'',
    mobile:'',
    dfImg:0,
    array: ['请选择','男', '女'],
    sexIndex:0,
    thisType:0,
    showCityPicker: false,
    citytab: 0,
    citytabs: ["请选择"],
    adressList: [],
    cityIndex: -1,
    addressName: '',
    proviceIndex: -1,
    cityIndex: -1,
    countyIndex: -1,
    countryIndex: -1,
    provice: null,
    city: null,
    county: null,
    country: null,
    animationAddressMenu: {},
    addressMenuIsShow: false, 
    addressA: { address:'请选择坐标'},
    action:"/api/address/userAddressInfo.do",
    addUrl:"/api/address/userSaveAddress.do",
    editUrl:"/api/address/userEditAddress.do",
    pidUrl:"/api/address/pidAdderssList.do",
	  submit:"{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 800,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    var self = this;
    ux.login(function (protocol) {
      if (protocol.success) {
        wx.setStorageSync("userInfo", protocol.data.userInfo);
        wx.setStorageSync("token", protocol.data.token);
        self.getAddressList(0);
        self.setData({
          userInfo: protocol.data.userInfo
        })
      }
    });
    this.animation = animation;
    if (options.id) {
      var $this = this;
      var parmas = { addressId: options.id}
      wx.showLoading({
        title: '加载中...',
      })
      ux.request(this.data.action, parmas, function (d) {
        $this.setData({
          details: d.data.address.detailAddress,
          userName: d.data.address.realName,
          mobile: d.data.address.mobile,
          dfImg: d.data.address.isDefault,
          addressA: { longitude: d.data.address.longitude, latitude: d.data.address.latitude, address: d.data.address.coordinate},
          gname: d.data.province.name + ">" + d.data.city.name + ">" + d.data.area.name + ">" + d.data.country.name,
          cityId: d.data.city.id,
          thisType: 4,
          provice: d.data.province.id,
          city: d.data.city.id,
          county: d.data.area.id,
          country: d.data.country.id,
          addressName: d.data.province.name + ">" + d.data.city.name + ">" + d.data.area.name + ">" + d.data.country.name,
       })
     
      wx.hideLoading();
      });
      this.setData({
        id:options.id
      })
    }    
  },
  
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  onChangeAddress: function (e) {
    wx.navigateTo({
      url: "/pages/member/tab3/adress/positon"
    });
  },
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

 
  bindPickerChange:function(e){
    this.setData({
      sexIndex: e.detail.value
    })
  },
  changDefault:function(){
    if(this.data.dfImg==0){
      this.setData({
       dfImg:1
      })
    }else{
      this.setData({
        dfImg: 0
      })
    }
   
  },
  userNames:function(e){
    var value = e.detail.value
    this.setData({
      userName:value
    })
  },
  detailsTap: function (e) {
    var value = e.detail.value
    this.setData({
      details: value
    })
  },
  mobileS:function(e){
    var value = e.detail.value
    this.setData({
      mobile: value
    })
  },
  tijiao:function(){
    var self=this;
    if (!self.data.userName){
      wx.showToast({
        title: '收货人姓名',
      })
      return false;
     }
    if (!self.data.mobile) {
      wx.showToast({
        title: '手机号码',
      })
      return false;
    }
    var regMobile = /^1\d{10}$/;
    if (!regMobile.test(self.data.mobile)) {
      wx.showToast({
        title: '手机格式有误！'
      })
      return false;
    }
    if (!self.data.country) {
      wx.showToast({
        title: '选择所在乡镇街道',
      })
      return false;
    }
    if (!self.data.details) {
      wx.showToast({
        title: '详细地址',
      })
      return false;
    }
    if (self.data.gname=='') {
      wx.showToast({
        title: '请选择详细坐标',
      })
      return false;
    }
 
    var pamars={
      mobile:this.data.mobile,
      realName:this.data.userName,
      isDefault:this.data.dfImg,
      cityId: Number(this.data.city),
      addressId: Number(this.data.country),
      detailAddress:this.data.details,
      longitude: this.data.addressA.longitude,
      latitude: this.data.addressA.latitude,
      coordinate: this.data.addressA.address,
    }
    var ursl="";
    if(this.data.id){
      pamars.id=this.data.id;
      ursl=this.data.editUrl;
    }else{
      ursl=this.data.addUrl;
    }
    ux.request(ursl, pamars, function (protocol) {
      self.toast(protocol.data.msg);
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面
      var prevPage = pages[pages.length - 2]; //上一个页面
      wx.navigateBack({
        delta: 1
      })
    }, true);
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  /**城市 开始*/

  getAddressList: function (pid) {
    console.log("dddddddd")
    var self = this;
    var params = { pid: pid, banrdId: this.data.banrdId };
    wx.showLoading({
      mask: true,
      title: '正在加载...'
    })
    ux.request('/api/address/getAddressListByPid.do', params, function (data) {
      wx.hideLoading();
      var adressList = self.data.adressList;
      adressList[self.data.citytab] = data.data.list
      self.setData({
        adressList: adressList
      })
    }, true);
  },
  showCityPicker: function () {
    this.setData({
      showCityPicker: true,
      hosList: []
    })
  },
  tabClick: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    this.setData({
      citytab: index
    })
    if (index == 0) {
      this.setData({
        selectsIndex: this.data.proviceIndex,
      })
    } else if (index == 1) {
      this.setData({
        selectsIndex: this.data.cityIndex,
      })
    } else if (index == 2) {
      this.setData({
        selectsIndex: this.data.countyIndex,
      })
    } else if (index == 3) {
      this.setData({
        selectsIndex: this.data.countryIndex,
      })
    }
  },
  selectThis: function (event) {
    var dataset = event.currentTarget.dataset;
    var idx = dataset.idx;
    var index = dataset.index;
    console.log(idx);
    var adressList = this.data.adressList;
    var tabs = this.data.citytabs;
    var id = adressList[idx][index].id;
    var tabName = adressList[idx][index].name;
    var newTabs = []
    var newList = []
    var idxtab = 0;

    if (idx == 0) {
      this.setData({
        provice: id,
        proviceIndex: index,
        city: null,
        county: null,
        country: null,
        cityIndex: -1,
        countyIndex: -1,
        countryIndex: -1,
      })
    } else if (idx == 1) {
      this.setData({
        city: id,
        county: null,
        country: null,
        cityIndex: index,
        countyIndex: -1,
        countryIndex: -1,
      })
    } else if (idx == 2) {
      this.setData({
        county: id,
        country: null,
        countyIndex: index,
        countryIndex: -1,
      })
    } else if (idx == 3) {
      this.setData({
        country: id,
        countryIndex: index,
      })
    }
    if (idx < 4) {
      for (var i = 0; i < tabs.length; i++) {
        newList[i] = adressList[i]
        if (i == idx) {
          newTabs[i] = tabName
          if (idx == 3) {
            idxtab = idx
            break;
          } else {
            newTabs.push("请选择");
            idxtab = idx + 1
            break;
          }
        } else {
          idxtab = idx;
          newTabs[i] = tabs[i]
        }
      }
      this.setData({
        adressList: newList,
        citytabs: newTabs,
        citytab: idxtab
      })
      if (idx != 3) {
        this.getAddressList(adressList[idx][index].id);
      } else {
        this.setData({
          selectsIndex: this.data.countryIndex,
        })
        this.okCityClick();
      }
    }
  },
  okCityClick: function () {
    var ps = '';
    var lists = this.data.adressList;

    if (this.data.proviceIndex >= 0) {
      ps = ps + lists[0][this.data.proviceIndex].name + ">";
    }
    if (this.data.cityIndex >= 0) {
      ps = ps + lists[1][this.data.cityIndex].name + ">";
    }
    if (this.data.countyIndex >= 0) {
      ps = ps + lists[2][this.data.countyIndex].name + ">";
    }
    if (this.data.countryIndex >= 0) {
      ps = ps + lists[3][this.data.countryIndex].name + ">";
    }
    this.setData({
      addressName: ps.substring(0, ps.length - 1),
      showCityPicker: false
    })
  },
  /**城市 结束*/
})
